```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring IoC容器源码深度解析 | 技术小馆</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.8;
        }
        .hero {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
            color: white;
        }
        .code-block {
            background: #282c34;
            border-radius: 8px;
            color: #abb2bf;
            font-family: 'Courier New', Courier, monospace;
        }
        .highlight {
            background: rgba(255, 215, 0, 0.2);
            padding: 2px 4px;
            border-radius: 4px;
        }
        .card {
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .section-divider {
            position: relative;
            height: 1px;
            background: linear-gradient(90deg, transparent, rgba(0,0,0,0.1), transparent);
            margin: 3rem 0;
        }
        .section-divider:after {
            content: '';
            position: absolute;
            left: 50%;
            top: -5px;
            transform: translateX(-50%);
            width: 10px;
            height: 10px;
            background: #6e8efb;
            border-radius: 50%;
        }
        .nav-link {
            transition: color 0.3s ease;
        }
        .nav-link:hover {
            color: #6e8efb;
        }
        .mermaid-container {
            background: white;
            border-radius: 12px;
            padding: 2rem;
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl text-center">
            <i class="fas fa-code-branch text-5xl mb-6 opacity-90"></i>
            <h1 class="text-4xl md:text-5xl font-bold mb-6 font-serif">Spring IoC容器源码深度解析</h1>
            <p class="text-xl md:text-2xl mb-8 max-w-3xl mx-auto opacity-90">探索Spring框架核心机制：控制反转与依赖注入的底层实现</p>
            <div class="flex justify-center space-x-4">
                <a href="#content" class="bg-white text-indigo-600 hover:bg-gray-100 px-6 py-3 rounded-full font-medium transition-all duration-300 shadow-md">
                    <i class="fas fa-book-open mr-2"></i>开始阅读
                </a>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-4 md:px-0 py-16" id="content">
        <!-- Introduction Section -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md p-8 card">
                <h2 class="text-3xl font-bold mb-6 text-gray-800 font-serif">Spring IoC容器概述</h2>
                <p class="text-lg mb-6">Spring框架的核心概念之一是 <span class="highlight">IoC（控制反转）容器</span>，它通过管理对象的生命周期和依赖关系，将控制权反转到容器中，帮助开发者实现松耦合、高可维护的系统。</p>
                <p class="text-lg">IoC容器通过 <span class="highlight">依赖注入（DI）</span> 的方式将需要的依赖关系注入到各个组件中，避免了传统的手动管理依赖的方式，极大提升了开发效率。</p>
            </div>
        </section>

        <!-- Mermaid Diagram -->
        <section class="mb-16 mermaid-container">
            <div class="text-center">
                <h3 class="text-2xl font-bold mb-6 text-gray-800 font-serif">Spring IoC 核心组件关系图</h3>
            </div>
            <div class="mermaid">
                graph TD
                    A[Spring IoC容器] --> B[BeanFactory]
                    A --> C[ApplicationContext]
                    B --> D[DefaultListableBeanFactory]
                    C --> E[ClassPathXmlApplicationContext]
                    C --> F[AnnotationConfigApplicationContext]
                    D --> G[BeanDefinition]
                    G --> H[GenericBeanDefinition]
                    G --> I[RootBeanDefinition]
                    D --> J[BeanPostProcessor]
                    J --> K[AutowiredAnnotationBeanPostProcessor]
                    D --> L[Dependency Injection]
                    L --> M[Constructor Injection]
                    L --> N[Setter Injection]
                    L --> O[Field Injection]
            </div>
        </section>

        <!-- Section 1 -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md p-8 card">
                <h2 class="text-3xl font-bold mb-6 text-gray-800 font-serif">1. ApplicationContext 和 BeanFactory 的区别及其源码实现</h2>
                
                <h3 class="text-2xl font-semibold mb-4 mt-8 text-gray-700">1.1. BeanFactory 的核心实现</h3>
                <p class="mb-6"><code>BeanFactory</code> 是 Spring IoC 容器的基础接口，最核心的功能是管理 Bean 的生命周期以及提供 Bean 实例。<code>BeanFactory</code> 的常见实现类是 <code>DefaultListableBeanFactory</code>。</p>
                
                <div class="code-block p-6 mb-6 overflow-x-auto">
                    <pre><code class="text-sm">public interface BeanFactory {
    Object getBean(String name) throws BeansException;
    boolean containsBean(String name);
    boolean isSingleton(String name);
}</code></pre>
                </div>
                
                <p class="mb-6">Spring 的 <code>BeanFactory</code> 提供了获取 Bean 的基本功能，<code>getBean()</code> 用于获取容器中指定名称的 Bean，<code>containsBean()</code> 用于判断容器中是否有指定名称的 Bean，<code>isSingleton()</code> 用于判断某个 Bean 是否是单例。</p>
                
                <p class="mb-4">在 <code>DefaultListableBeanFactory</code> 中，<code>getBean()</code> 方法的实现如下：</p>
                
                <div class="code-block p-6 mb-6 overflow-x-auto">
                    <pre><code class="text-sm">public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements ConfigurableListableBeanFactory {
    @Override
    public Object getBean(String name) throws BeansException {
        BeanDefinition beanDefinition = getBeanDefinition(name);  // 获取 Bean 定义
        Object bean = createBean(name, beanDefinition);  // 根据 Bean 定义创建实例
        return bean;
    }
}</code></pre>
                </div>
                
                <ul class="list-disc pl-6 mb-6 space-y-2">
                    <li><code>getBean()</code> 方法首先通过 <code>getBeanDefinition()</code> 获取 Bean 的定义信息（即 <code>BeanDefinition</code>）。</li>
                    <li>然后，调用 <code>createBean()</code> 方法，依赖于 <code>BeanDefinition</code> 来创建具体的 Bean 实例。</li>
                </ul>
                
                <h3 class="text-2xl font-semibold mb-4 mt-8 text-gray-700">1.2. ApplicationContext 的扩展与增强</h3>
                <p class="mb-6"><code>ApplicationContext</code> 是 <code>BeanFactory</code> 的子接口，提供了更多的功能，除了基本的 Bean 管理外，还支持事件传播、消息资源访问等功能。<code>ClassPathXmlApplicationContext</code> 是 <code>ApplicationContext</code> 的常见实现之一。</p>
                
                <div class="code-block p-6 mb-6 overflow-x-auto">
                    <pre><code class="text-sm">public interface ApplicationContext extends BeanFactory {
    Object getBean(String name) throws BeansException;
    &lt;T&gt; T getBean(Class&lt;T&gt; requiredType) throws BeansException;
    String[] getBeanDefinitionNames();
}</code></pre>
                </div>
                
                <p class="mb-6"><code>ApplicationContext</code> 继承自 <code>BeanFactory</code>，并且提供了更多的功能，例如事件发布和资源加载。<code>getBean()</code> 方法支持通过名称或类型来获取 Bean，<code>getBeanDefinitionNames()</code> 方法用于获取容器中所有 Bean 的定义名称。</p>
            </div>
        </section>

        <div class="section-divider"></div>

        <!-- Section 2 -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md p-8 card">
                <h2 class="text-3xl font-bold mb-6 text-gray-800 font-serif">2. BeanDefinition 的定义与 Bean 的实例化</h2>
                
                <h3 class="text-2xl font-semibold mb-4 mt-8 text-gray-700">2.1. BeanDefinition 解析</h3>
                <p class="mb-6"><code>BeanDefinition</code> 是描述 Bean 配置信息的元数据类，它包含了 Bean 的各种配置信息，如类名、作用域、初始化方法等。</p>
                
                <div class="code-block p-6 mb-6 overflow-x-auto">
                    <pre><code class="text-sm">public class GenericBeanDefinition implements BeanDefinition {
    private String beanClassName;
    private Class&lt;?&gt; beanClass;
    private boolean singleton = true;
    
    public void setBeanClass(Class&lt;?&gt; beanClass) {
        this.beanClass = beanClass;
    }

    public void setBeanClassName(String beanClassName) {
        this.beanClassName = beanClassName;
    }

    public String getBeanClassName() {
        return this.beanClassName;
    }

    public Class&lt;?&gt; getBeanClass() {
        return this.beanClass;
    }

    public boolean isSingleton() {
        return this.singleton;
    }
}</code></pre>
                </div>
                
                <ul class="list-disc pl-6 mb-6 space-y-2">
                    <li><code>GenericBeanDefinition</code> 是 <code>BeanDefinition</code> 的一个实现，表示 Bean 的定义。它包含了 Bean 的类名 (<code>beanClassName</code>)、类对象 (<code>beanClass</code>)，以及作用域标识（<code>singleton</code>）等属性。</li>
                    <li><code>setBeanClass()</code> 方法用来设置 Bean 的类对象，<code>getBeanClass()</code> 用来获取 Bean 的类对象。</li>
                </ul>
                
                <h3 class="text-2xl font-semibold mb-4 mt-8 text-gray-700">2.2. Bean 实例化与依赖注入</h3>
                <p class="mb-6">当 <code>BeanDefinition</code> 被解析后，Spring 通过 <code>AutowireCapableBeanFactory</code> 来实例化和注入依赖。</p>
                
                <div class="code-block p-6 mb-6 overflow-x-auto">
                    <pre><code class="text-sm">public class AutowireCapableBeanFactory extends AbstractAutowireCapableBeanFactory {
    @Override
    protected Object createBean(String beanName, BeanDefinition beanDefinition) throws BeansException {
        Class&lt;?&gt; beanClass = beanDefinition.getBeanClass();
        try {
            Object bean = beanClass.getDeclaredConstructor().newInstance();  // 实例化 Bean
            applyPropertyValues(beanName, bean, beanDefinition);  // 进行依赖注入
            return bean;
        } catch (Exception ex) {
            throw new BeansException("Error creating bean", ex);
        }
    }
}</code></pre>
                </div>
                
                <ul class="list-disc pl-6 mb-6 space-y-2">
                    <li>在 <code>createBean()</code> 方法中，首先通过 <code>getBeanClass()</code> 获取 Bean 的类对象，然后使用反射来实例化该 Bean。</li>
                    <li><code>applyPropertyValues()</code> 方法会根据 Bean 的定义，给 Bean 注入依赖。</li>
                </ul>
            </div>
        </section>

        <div class="section-divider"></div>

        <!-- Section 3 -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md p-8 card">
                <h2 class="text-3xl font-bold mb-6 text-gray-800 font-serif">3. 依赖注入的实现</h2>
                
                <h3 class="text-2xl font-semibold mb-4 mt-8 text-gray-700">3.1. 依赖注入的类型</h3>
                <p class="mb-6">Spring 提供了三种主要的依赖注入方式：</p>
                
                <div class="grid md:grid-cols-3 gap-6 mb-6">
                    <div class="bg-indigo-50 p-6 rounded-lg">
                        <div class="text-indigo-600 text-2xl mb-3">
                            <i class="fas fa-cube"></i>
                        </div>
                        <h4 class="font-bold text-lg mb-2">构造器注入</h4>
                        <p>通过构造器传入依赖</p>
                    </div>
                    <div class="bg-indigo-50 p-6 rounded-lg">
                        <div class="text-indigo-600 text-2xl mb-3">
                            <i class="fas fa-wrench"></i>
                        </div>
                        <h4 class="font-bold text-lg mb-2">Setter方法注入</h4>
                        <p>通过Setter方法传入依赖</p>
                    </div>
                    <div class="bg-indigo-50 p-6 rounded-lg">
                        <div class="text-indigo-600 text-2xl mb-3">
                            <i class="fas fa-code"></i>
                        </div>
                        <h4 class="font-bold text-lg mb-2">字段注入</h4>
                        <p>通过字段直接注入依赖</p>
                    </div>
                </div>
                
                <p class="mb-6"><code>@Autowired</code> 注解通常与这些方式结合使用，Spring 会根据 Bean 的配置和注解自动进行注入。</p>
                
                <h3 class="text-2xl font-semibold mb-4 mt-8 text-gray-700">3.2. 依赖解析与注入</h3>
                <p class="mb-6"><code>AutowiredAnnotationBeanPostProcessor</code> 负责处理 <code>@Autowired</code> 注解，并将依赖注入到 Bean 中。</p>
                
                <div class="code-block p-6 mb-6 overflow-x-auto">
                    <pre><code class="text-sm">public class AutowiredAnnotationBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        injectAutowire(bean);
        return bean;
    }

    private void injectAutowire(Object bean) throws BeansException {
        for (Field field : bean.getClass().getDeclaredFields()) {
            if (field.isAnnotationPresent(Autowired.class)) {
                Object dependency = getBean(field.getType());  // 获取依赖的 Bean
                field.setAccessible(true);
                try {
                    field.set(bean, dependency);  // 注入依赖
                } catch (IllegalAccessException e) {
                    throw new BeansException("Failed to inject dependency", e);
                }
            }
        }
    }
}</code></pre>
                </div>
                
                <ul class="list-disc pl-6 mb-6 space-y-2">
                    <li><code>postProcessBeforeInitialization()</code> 方法在 Bean 初始化前调用，检查 <code>@Autowired</code> 注解并执行注入。</li>
                    <li><code>injectAutowire()</code> 方法会遍历 Bean 的字段，找到 <code>@Autowired</code> 注解的字段并注入相应的依赖。</li>
                </ul>
            </div>
        </section>

        <div class="section-divider"></div>

        <!-- Section 4 -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md p-8 card">
                <h2 class="text-3xl font-bold mb-6 text-gray-800 font-serif">4. BeanPostProcessor 与 Bean 初始化过程</h2>
                
                <p class="mb-6"><code>BeanPostProcessor</code> 是 Spring IoC 容器的扩展点，允许开发者在 Bean 初始化的前后进行自定义处理。例如，Spring 在初始化 Bean 之前会调用 <code>postProcessBeforeInitialization()</code>，初始化之后会调用 <code>postProcessAfterInitialization()</code>。</p>
                
                <div class="code-block p-6 mb-6 overflow-x-auto">
                    <pre><code class="text-sm">public class MyBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof MyBean) {
            System.out.println("Before initialization: " + beanName);
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof MyBean) {
            System.out.println("After initialization: " + beanName);
        }
        return bean;
    }
}</code></pre>
                </div>
                
                <ul class="list-disc pl-6 mb-6 space-y-2">
                    <li>在 <code>postProcessBeforeInitialization()</code> 中，我们可以对 Bean 进行一些操作，例如日志记录、校验等。</li>
                    <li>在 <code>postProcessAfterInitialization()</code> 中，我们可以对 Bean 进行进一步的处理，如包装 Bean、增强功能等。</li>
                </ul>
            </div>
        </section>

        <div class="section-divider"></div>

        <!-- Section 5 -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md p-8 card">
                <h2 class="text-3xl font-bold mb-6 text-gray-800 font-serif">5. Spring IoC 容器的生命周期管理</h2>
                
                <p class="mb-6">Spring IoC 容器通过多个生命周期钩子函数来管理 Bean 的生命周期。主要的生命周期钩子函数有：</p>
                
                <ul class="list-disc pl-6 mb-6 space-y-2">
                    <li><strong>Bean 初始化</strong>：通过 <code>@PostConstruct</code> 注解或者 <code>InitializingBean</code> 接口的 <code>afterPropertiesSet()</code> 方法进行初始化操作。</li>
                    <li><strong>Bean 销毁</strong>：通过 <code>@PreDestroy</code> 注解或者 <code>DisposableBean</code> 接口的 <code>destroy()</code> 方法进行销毁操作。</li>
                </ul>
                
                <div class="code-block p-6 mb-6 overflow-x-auto">
                    <pre><code class="text-sm">public class MyBean implements InitializingBean, DisposableBean {
    @Override
    public void afterPropertiesSet() throws Exception {
        // 初始化代码
    }

    @Override
    public void destroy() throws Exception {
        // 销毁代码
    }
}</code></pre>
                </div>
                
                <ul class="list-disc pl-6 mb-6 space-y-2">
                    <li><code>afterPropertiesSet()</code> 方法在所有属性设置完成后被调用，进行初始化操作。</li>
                    <li><code>destroy()</code> 方法在容器销毁 Bean 时被调用，用于清理资源。</li>
                </ul>
            </div>
        </section>

        <!-- Summary Section -->
        <section class="mb-16">
            <div class="bg-gradient-to-r from-indigo-500 to-purple-600 rounded-xl shadow-md p-8 text-white">
                <h2 class="text-3xl font-bold mb-6 font-serif">核心要点总结</h2>
                <ul class="list-disc pl-6 space-y-3">
                    <li>Spring IoC容器通过<strong>BeanFactory</strong>和<strong>ApplicationContext</strong>提供依赖管理和控制反转功能</li>
                    <li><strong>BeanDefinition</strong>是描述Bean配置的元数据，包含了类名、作用域等关键信息</li>
                    <li>Spring支持<strong>构造器注入</strong>、<strong>Setter注入</strong>和<strong>字段注入</strong>三种依赖注入方式</li>
                    <li><strong>BeanPostProcessor</strong>接口允许在Bean初始化前后进行自定义处理</li>
                    <li>Spring通过<strong>生命周期回调</strong>方法管理Bean的初始化和销毁过程</li>
                </ul>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12 px-4">
        <div class="container mx-auto max-w-5xl text-center">
            <div class="mb-6">
                <h3 class="text-xl font-bold mb-2">技术小馆</h3>
                <p class="text-gray-400">深入解析技术原理，分享开发经验</p>
            </div>
            <div class="mb-6">
                <a href="http://www.yuque.com/jtostring" class="text-indigo-400 hover:text-indigo-300 transition-colors duration-300">
                    <i class="fas fa-book mr-2"></i>访问我们的语雀知识库
                </a>
            </div>
            <div class="text-sm text-gray-500">
                &copy; 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>
```